<?php
/**
 * 服务基类
 * User: 13651
 * Time: 2025/9/12 10:34
 */

namespace App\Services;

class BaseService
{
    protected static $instance = [];

    /**
     * @return static
     */
    public static function getInstance()
    {
        if((static::$instance[static::class] ?? []) instanceof static) {
            return static::$instance[static::class];
        }
        return static::$instance[static::class] = new static();
    }

    /**
     * 组装列表返回格式
     * @param $coll
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    protected function toReturnList($coll, int $page = 0, int $pageSize = 20)
    {
        return [
            'total' => $coll->count(),
            'list' => $coll->offset($page * $pageSize)->limit($pageSize)->get()
        ];
    }

    /**
     * 构建简单的列表数据
     * @param $coll
     * @param int $page
     * @param int $pageSize
     * @return mixed
     */
    protected function toSimpleList($coll, int $page = 0, int $pageSize = 20)
    {
        return $coll->offset($page * $pageSize)->limit($pageSize)->get();
    }

    private function __construct(){}
    private function __clone(){}
}